import wx
from wx.lib.agw.ribbon import panel_extension_xpm


#自定义窗口类（计算器）
class Calculate(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None, title='计算器', size=(300, 400))

        #初始化界面UI
        self.init_ui()

        #初始化数据
        self.init_data()

        #居中显示
        self.Center()
        self.Show()

    #初始化UI界面
    def init_ui(self):
        panel = wx.Panel(self)

        #垂直方向布局管理器
        vbox = wx.BoxSizer(wx.VERTICAL)

        #显示内容的显示框
        self.tc_content = wx.TextCtrl(panel, style=wx.TE_RIGHT | wx.TE_READONLY)

        #宽度自适应，-1表示自适应
        self.tc_content.SetMinSize((-1, 50))

        #控件在主轴方向是否拉伸  0:表示不拉伸
        #wx.EXPAND:表示拉伸
        #wx.ALL:表示四个方向都拉伸
        vbox.Add(self.tc_content,0,wx.EXPAND | wx.ALL, 5)

        #网格布局管理器
        #参数1:网格行数，参数2:网格列数，参数3:行间距，参数4:列间距
        grid = wx.GridSizer(5, 4, 5, 5)

        #按钮
        btn_list = [('c',self.on_clear_click), ('+/-',self.on_operate_click), ('%',self.on_operate_click), ('/',self.on_operate_click),
                    ('7',self.on_number_click), ('8',self.on_number_click), ('9',self.on_number_click), ('*',self.on_operate_click),
                    ('4',self.on_number_click), ('5',self.on_number_click), ('6',self.on_number_click), ('-',self.on_operate_click),
                    ('1',self.on_number_click), ('2',self.on_number_click), ('3',self.on_number_click), ('+',self.on_operate_click),
                    ('0',self.on_number_click), ('.',self.on_operate_click), ('=',self.on_equal_click)
        ]

        #循环创建按钮
        for label,handler in btn_list:
            #绑定按钮的单机事件
            btn = wx.Button(panel, label=label)
            btn.Bind(wx.EVT_BUTTON,handler)

            grid.Add(btn, 0, wx.EXPAND | wx.ALL, 3)

        vbox.Add(grid, 1, wx.EXPAND)


        #将垂直方向布局管理器添加到面板中
        panel.SetSizer(vbox)


    def init_data(self):
        self.current_value = '0'  #存储当前显示的值
        self.is_fisrt = True   #标识是否是第一次输入
        self.fisrt_num = None  #第一个数字
        self.operate = None

    #数字按钮的点击事件处理方法
    def on_number_click(self,event):
        #获取点击的数字
        num = event.GetEventObject().GetLabel()
        if self.is_fisrt:
            self.current_value = num
            self.is_fisrt = False
        else:
            self.current_value += num

        self.update_content()

    #跟新界面上的内容
    def update_content(self):
        self.tc_content.SetLabelText(str(self.current_value))

    #运算按钮的点击事件处理方法
    def on_operate_click(self,event):
        op = event.GetEventObject().GetLabel()  #获取运算符
        #判断是否第一次点击运算符，如果不是则进行运算
        if self.operate and not self.is_fisrt:
            self.calculate()


        self.fisrt_num = float(self.current_value)
        self.operate = op
        self.is_fisrt = True

    #清除
    def on_clear_click(self,event):
        self.init_data()
        self.update_content()

    #点击等于号时参与计算
    def calculate(self):
        second_num = float(self.current_value)
        if self.operate == '+':
            result = self.fisrt_num + second_num
        elif self.operate == '-':
            result = self.fisrt_num - second_num
        elif self.operate == '*':
            result = self.fisrt_num * second_num
        elif self.operate == '/':
            result = self.fisrt_num / second_num
        self.current_value = str(result)
        self.update_content()

    #等于号点击事件
    def on_equal_click(self,event):
        self.calculate()
        self.operate = None


if __name__ == '__main__':
    app = wx.App()
    frame = Calculate()
    app.MainLoop()